#include <iostream>
using namespace std;

const int N = 100010;

int a[N], tem[N];
int n;
unsigned long long m = 0;

void merge_sort(int a[], int r, int l)
{
    if (r >= l)
        return;
    int mid = r + l >> 1;
    merge_sort(a, r, mid), merge_sort(a, mid + 1, l);
    int k = 0, i = r, j = mid + 1;
    while (i <= mid && j <= l)
        if (a[i] <= a[j])
            tem[k++] = a[i++];
        else
            tem[k++] = a[j++], m += mid + 1 - i;
    while (i <= mid)
        tem[k++] = a[i++];
    while (j <= l)
        tem[k++] = a[j++];
    for (i = r, j = 0; i <= l; ++i, ++j)
        a[i] = tem[j];
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d", a + i);
    merge_sort(a, 0, n - 1);
    printf("%d\n", m);
    for (int i = 0; i < n; ++i)
        printf("%lld ", a[i]);
    return 0;
}